package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.EnterpriseList;
import com.isunland.managesystem.entity.EnterpriseListOriginal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EnterpriseListFragment extends BaseListFragment {

  public static final String EXTRA_TYPE = "com.isunland.managesystem.ui.EXTRA_TYPE";
  public static final String EXTRA_ENTERPRISE = "com.isunland.managesystem.ui.EXTRA_ENTERPRISE";
  //供应商(supplier),客户(customer),其他往来单位(otherCustomer)）
  public static final String VALUE_SUPPLIER = "supplier";
  public static final String VALUE_CUSTOMER = "customer";
  public static final String VALUE_OTHER_CUSTOMER = "otherCustomer";
  private BaseListAdapter mAdapter;
  private String mType;//单位类别

  @Override public void onCreate(Bundle savedInstanceState) {
    mType = getArguments().getString(EXTRA_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    // Auto-generated method stub
    return ApiConst.URL_ENTERPRISE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("page", "1");
    params.put("rows", "10000");
    params.put("type", mType);

    return params;
  }

  @Override public void resolveResponse(String json) {

    EnterpriseListOriginal original = new Gson().fromJson(json, EnterpriseListOriginal.class);
    List<EnterpriseList> rows = original.getRows();
    setAdapter(rows);
  }

  private void setAdapter(List<EnterpriseList> detail) {
    if (detail == null) {
      detail = new ArrayList<EnterpriseList>();
    }

    if (mAdapter == null) {
      mAdapter = new BaseListAdapter<EnterpriseList>(getActivity(), detail) {

        @Override
        public void bindData(int position, BaseListAdapter.ViewHolder vh, EnterpriseList item) {
          vh.mTitleOneTv.setText(getContext().getString(R.string.enterpriseName));
          vh.mContentOneTv.setText(item.getEnterpriseName());
          vh.mTitleTwoTv.setText(getContext().getString(R.string.communicatePerson));
          vh.mContentTwoTv.setText(item.getContactName());
          vh.mTitleThreeTv.setText(getContext().getString(R.string.communicatePhone));
          vh.mContentThreeTv.setText(item.getLinkPhone());
        }
      };
      setListAdapter(mAdapter);
      return;
    }

    mAdapter.clear();
    mAdapter.addAll(detail);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    /**
     * 	Intent i = new Intent(getActivity(), ReceiptPagerActivity.class);
     i.putExtra(ReceiptInfoFragment.EXTRA_RECEIPT, mAdapter.getItem(position - 1));
     startActivityForResult(i, REQUEST_DETAIL);
     super.onListItemClick(l, v, position, id);
     */
    // Auto-generated method stub
    EnterpriseList item = (EnterpriseList) mAdapter.getItem(position - 1);
    item.setCustomerType(mType);
    Intent i = new Intent();
    i.putExtra(EXTRA_ENTERPRISE, item);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_refresh, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case R.id.menu_item_refresh:

        volleyPost();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public void refresh() {
    volleyPost();
  }

  public static EnterpriseListFragment newInstance(String type) {
    Bundle args = new Bundle();
    args.putString(EXTRA_TYPE, type);
    EnterpriseListFragment fragment = new EnterpriseListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
